package synchronized方法与锁对象;
class MyObject{
	synchronized public void methodA()//调用synchronized,使方法排队进行
									  //只有共享资源的读写访问需要同步化，如果不是共享资源那么没有同步地必要
	{
		try
		{
			System.out.println("begin methodA threadName="+Thread.currentThread().getName());
			Thread.sleep(5000);
			System.out.println("end");
		}
		catch(InterruptedException e)
		{
			e.printStackTrace();
		}
		
	}
}
class ThreadA extends Thread{
	private MyObject object;
	public ThreadA(MyObject object)
	{
		super();
		this.object=object;
	}
	public void run()
	{
		super.run();
		object.methodA();
	}
}
class ThreadB extends Thread{
	private MyObject object;
	public ThreadB(MyObject object)
	{
		super();
		this.object=object;
	}
	public void run()
	{
		super.run();
		object.methodA();
	}
}
public class Test2_6 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MyObject object=new MyObject();
		ThreadA a=new ThreadA(object);
		a.setName("A");
		ThreadB b=new ThreadB(object);
		b.setName("B");
		a.start();
		b.start();
		
	}

}
